package com.lightonyoga;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class LightOnYogaActivity extends Activity {
	/** Called when the activity is first created. */
	String TAG="LightOnYoga:LightOnYoga";
	ArrayList<Week> weeks;
	private static ListView lvmain;
	private static ArrayList<String> lv_arr;
	static Context  context;
	public static ArrayList<Aasan> aasans = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		context = this.getApplicationContext();
		lvmain = (ListView)  findViewById(R.id.listViewMain);
		lvmain.setClickable(true);

        View header = getLayoutInflater().inflate(R.layout.headerlayoutmain, null);
        lvmain.addHeaderView(header, null, false);

		//Show UI to users
		makeDisplayReady();
	}

	void makeDisplayReady() {
		//load the list view
		loadWeeks();
		//displayWeeks
		displayWeeks();
		//add a event listener for list view clicks.
		attachEventListenerToList();
	}

	void loadWeeks() {
		InputStream fin;
		DataInputStream in;
		BufferedReader br ;
		String line;
		ArrayList<String> aasan;
		String strArray[];
		String weekName;
		Week newWeek;
		int i;
		//read the Week file and store it in the array list
		weeks = new ArrayList<Week>();
		try {
			fin =  this.getAssets().open("weeks.csv",AssetManager.ACCESS_BUFFER);
			in = new DataInputStream(fin);
			br  = new BufferedReader(new InputStreamReader(in),8196);
			br.readLine(); //skip the first line as it is a header
			//now read the file line by line
			while ((line=br.readLine()) != null){
				aasan = new ArrayList<String>();
				strArray = line.split(","); //tokenizer
				weekName = strArray[0];
				i = 1; //0 index is the name of the week.
				while (i<strArray.length) {
					if(strArray[i]==null) //if there are no more aasan's left
						break;
					aasan.add(strArray[i]);
					i++;
				}
				newWeek = new Week(weekName,aasan);
				weeks.add(newWeek);
			}
			fin.close();

		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	void displayWeeks() {
		lv_arr = new ArrayList<String>();
		for (int i=0; i<weeks.size();i++) {
			lv_arr.add("Week "+weeks.get(i).getWeekNum());
		}
		lvmain.setAdapter(new ListAdapterMain(this, R.layout.list_view,lv_arr));
	}


	void attachEventListenerToList(){
		//pass the week object to displayWeekActivity on user click as a bundle

		//starts another activity to show the detail results for a Device
		lvmain.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
				//week is in format week xxx
				Week toSend = null;
				String week = ((String)lvmain.getItemAtPosition(pos)).substring(5);
				Log.i(TAG, "OnItemClickListener you clicked position " + pos +" and the contect was "+ lvmain.getItemAtPosition(pos) + "  and week is -"+ week+"-");
				Bundle bundle = new Bundle();
				//add the weeks data Structure
				for (int i=0; i<weeks.size();i++) {
					if(weeks.get(i).getWeekNum().compareTo(week)==0) {
						toSend = weeks.get(i);
						break;
					}
				}

				if(toSend == null) {
					Log.i(TAG,"toSend is null");
				}
				bundle.putSerializable("week",  toSend);
				Intent newIntent = new Intent(context, ShowWeekDetails.class);
				newIntent.putExtras(bundle);
				startActivityForResult(newIntent,0);
			}
		}) ;
	}
	
	public static void loadAasans() {
		InputStream fin;
		DataInputStream in;
		BufferedReader br ;
		String line;
		ArrayList<String> aasanImages;
		String strArray[];
		Aasan newAasan;
		
		int i;
		//read the Week file and store it in the array list
		aasans = new ArrayList<Aasan>();
		try {
			fin =  context.getAssets().open("aasan.csv",AssetManager.ACCESS_BUFFER);
			in = new DataInputStream(fin);
			br  = new BufferedReader(new InputStreamReader(in),8196);
			br.readLine(); //skip the first line as it is a header
			//now read the file line by line
			while ((line=br.readLine()) != null){
				aasanImages = new ArrayList<String>();
				strArray = line.split(";"); //tokenizer
				i = 6; //0 index is the name of the week.
				while (i<strArray.length) {
					if(strArray[i]==null) //if there are no more aasan's left
						break;
					aasanImages.add(strArray[i]);
					i++;
				}
				
				newAasan = new Aasan(strArray[0],strArray[1],strArray[2],strArray[3],strArray[4],strArray[5],aasanImages);
				newAasan.printSansName();
				aasans.add(newAasan);
			}
			fin.close();

		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}